using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Threading;



namespace _3D_FileExplorer
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Interface : DrawableGameComponent
    {
        public static string messageBoard = "";
        SpriteFont font;
        public SpriteBatch spriteBatch;
        Texture2D levelUP;
        Texture2D textBoxBasis;
        Texture2D pathText;
        ContentManager content;
        Rectangle levelUP_pos;
        Rectangle textBoxBasis_pos;
        Rectangle pathText_pos;
        Rectangle levelUP_text;
        Rectangle mousePosition;
        GraphicsDevice graphicsDevice;
        public static Vector2 messageBoardPosition = new Vector2(20f, 30f);
        Texture2D textBack;
        Camera camera;
        CameraAnimation cameraAnimation;
        private bool clickEnable = true;




        public Interface(Game game,ContentManager content,Camera camera)
            : base(game)
        {
            this.content = content;
            this.camera = camera;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
         
            // TODO: Add your initialization code here

            
            base.Initialize();

        }
        protected override void LoadContent()
        {
           this.graphicsDevice = GraphicsDevice;
            textBack = content.Load<Texture2D>(@"Textures\textback");
            font = content.Load<SpriteFont>(@"Fonts/Arial");
            levelUP = content.Load<Texture2D>(@"Textures/2D_Interface/levelup");
            textBoxBasis = content.Load<Texture2D>(@"Textures/2D_Interface/textBoxBasis");
            pathText = content.Load<Texture2D>(@"Textures/2D_Interface/pathText");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            levelUP_pos.X = Fe.canvas.Width - 95;
            levelUP_pos.Y = 20;
            textBoxBasis_pos.X = 21;
            textBoxBasis_pos.Y = 20;
            textBoxBasis_pos.Width = 700;
            textBoxBasis_pos.Height = 42;
            pathText_pos.X = 40;
            pathText_pos.Y = 32;
            pathText_pos.Width = 43;
            pathText_pos.Height = 18;
            levelUP_pos.Width = 74;
            levelUP_pos.Height = 42;
            levelUP_text.Height = 42;
            levelUP_text.Width = 74;
            mousePosition.X = Mouse.GetState().X;
            mousePosition.Y = Mouse.GetState().Y;
            mousePosition.Height = 1;
            mousePosition.Width = 1;
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            levelUP_pos.X = Fe.canvas.Width - 95;
            mousePosition.X = Mouse.GetState().X;
            mousePosition.Y = Mouse.GetState().Y;
            mousePosition.Height = 1;
            mousePosition.Width = 1;
            // TODO: Add your update code here
            if (levelUP_pos.Intersects(mousePosition))
            {
                levelUP_text.Y = 42;
                if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                {
                    if (clickEnable)
                    {
                        DrawGraphicsTree.CreateNewThread(GraphicsDevice, 0);
                        clickEnable = false;
                    }
                }
                if (Mouse.GetState().LeftButton == ButtonState.Released)
                {
                    clickEnable = true;
                }

            }
            else
            {
                levelUP_text.Y = 0;
            }



            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            spriteBatch.Begin();
            //Draw text with background
            float extension;
            Vector2 textBackScale;
            if ((1.5f - (messageBoard.Length * 0.05f)) < 1)
            {
                extension = 1;
            }
            else
            {
                extension = (1.5f - (messageBoard.Length * 0.05f));
            }

            if (messageBoard.Length > 0)
            {
                textBackScale.X = messageBoard.Length * 0.0175f * extension;
                textBackScale.Y = 0.18f;
                spriteBatch.Draw(textBack, messageBoardPosition, null, new Color(Color.YellowGreen, 0.8f), 0, Vector2.Zero, textBackScale, SpriteEffects.None, 1);
                spriteBatch.DrawString(font, messageBoard, new Vector2(messageBoardPosition.X + 2f, messageBoardPosition.Y + 1f), Color.Black, 0, Vector2.Zero, 0.2f, SpriteEffects.None, 0);
            }
            //spriteBatch.Draw(levelUP, levelUP_pos, Color.White);
            spriteBatch.Draw(levelUP, levelUP_pos, levelUP_text, new Color(Color.White, 0.9f), 0, Vector2.Zero, SpriteEffects.None, 1);
            spriteBatch.Draw(textBoxBasis, textBoxBasis_pos,null, new Color(Color.White,0.9f), 0, Vector2.Zero, SpriteEffects.None, 1);
            spriteBatch.Draw(pathText, pathText_pos, null, new Color(Color.White, 0.9f), 0, Vector2.Zero, SpriteEffects.None, 1);


           
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public static void SetMessageBoardPosition3D(Vector3 point,Camera camera,GraphicsDevice graphicsDevice)
        {
           Vector3 screenSpace = graphicsDevice.Viewport.Project(point, camera.projection, camera.view, Matrix.Identity);
           messageBoardPosition.X = screenSpace.X+10f;
           messageBoardPosition.Y = screenSpace.Y;

        }

        public static void SetMessageBoardPosition2D(Vector2 point)
        {
            messageBoardPosition.X = point.X;
            messageBoardPosition.Y = point.Y;

        }
    }
}